/*
 * 作者: 侯晓宇
 * 时间: 2017-02-12
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {

	var c = require('app/utils/CommonUtils');
	var dbu = require('app/utils/DBUtils');
	var fs = api.require('fs');
	var stop = true;
	var s = {};

	s.imageCache = function(remote_image_url){
		var remote_url = remote_image_url;
		
		var cache_url = dbu.getImageCache(remote_image_url);
		
		if(c.isNullStr(cache_url) || cache_url=="null"){
			
			api.imageCache({
			    url: remote_url
			}, function(ret, err) {
				
			    c.debug("remote_url",remote_url);
			    c.debug("cache_url",cache_url);
			    c.debug("ret",ret);
				c.debug("err",err);
				
				if(c.isNull(ret.url) || ret.url == remote_image_url){
					return ;
				}else{
					dbu.saveImageCache(remote_url,ret.url);	
				}
			});
			
			return remote_url;
		}else{
			var ret = fs.existSync({
			    path: cache_url
			});
			if (ret.exist) {
				return cache_url;
			} else {
				dbu.removeImageCache(remote_image_url);
			    return remote_url;
			}
		}
	}
	
	s.deleteImageCache = function(){
		dbu.resetImageCache();
	}

	return s;
});